//
//  SettingViewController.m
//  work
//
//  Created by 徐宏飞 on 16/5/20.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "SettingViewController.h"
#import "XHF_SMSLoginViewController.h" //验证码登陆
#import "PushMessageViewController.h"  //推送消息
#import "AboutUsViewController.h"      //关于我们
#import "XRCarouselView.h"             //轮播图
#import "SettingTableViewCell.h"  
#import "XHF_WebViewController.h"


static NSString *settingTableViewCell_ID = @"SettingTableViewCell";



@interface SettingViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *settingTableView;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (nonatomic, strong) ZhuangxiuManager *manger ;


@end

@implementation SettingViewController



#pragma mark - life cycle
- (void)initNavigationBar
{
    self.enableReturnButton = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"设置";
    
    self.view.backgroundColor = RGBA(239, 239, 244, 1);
    
    [self.settingTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.settingTableView registerNib:[UINib nibWithNibName:@"SettingTableViewCell" bundle:nil] forCellReuseIdentifier:settingTableViewCell_ID];
    
    self.settingTableView.scrollEnabled = NO;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

     self.manger = [ZhuangxiuManager shareManager];
    
    //判断登录状态
    [self judgeLoginState];
}




#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.font = FW_XWID_FONT(17);
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"推送消息设置";
        } else {
            //获取缓存数据
            NSUInteger size = [[SDImageCache sharedImageCache] getSize];
            SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:settingTableViewCell_ID];
//            cell.textLabel.text = @"清除缓存";
            cell.lblSize.text = [NSString stringWithFormat:@"%.02lfM", size / 1024.0 / 1024.0];
            return cell;
        }
    } else {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"关于装修精灵";
        } else {
            cell.textLabel.text = @"用户协议";
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            PushMessageViewController *pushMsg = [PushMessageViewController new];
            [self.navigationController pushViewController:pushMsg animated:YES];
        } else {
            //获取缓存数据
            NSUInteger size = [[SDImageCache sharedImageCache] getSize];
            //提示
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"缓存为%.02lfM\n 您确定删除吗?", size / 1024.0 / 1024.0] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *acc = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull            action) {
                //清除图片
                if (size != 0) {
                    [[SDImageCache sharedImageCache] clearDisk];
                    [XRCarouselView clearDiskCache];
 
                }
                NSIndexPath *te=[NSIndexPath indexPathForRow:1 inSection:0];
                [self.settingTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:te,nil] withRowAnimation:UITableViewRowAnimationFade];

                
            }];
            
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            
            [alert addAction:acc];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:^{
            }];
            
            
            
            
        }
    } else {
        if (indexPath.row == 0) {
            AboutUsViewController *aboutUs = [AboutUsViewController new];
            [self.navigationController pushViewController:aboutUs animated:YES];
        } else {
            
            //进入web
            XHF_WebViewController *web = [XHF_WebViewController new];
            
            web.url = @"https://www.baidu.com";
            web.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:web animated:YES];
            
            
        }
        
    }


}





#pragma mark - CustomDelegate
#pragma mark - event response
- (IBAction)loginBtnAction:(id)sender {
    
    
    ZhuangxiuManager *manger = [ZhuangxiuManager shareManager];
    self.loginBtn.hidden = YES;

    //将本地的数据清空
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loginData"];
    
    manger.loginSucceed = @"0";
    
    [[DeviceCheckIn shareManager] startRequst];
    
    
    [self.navigationController popViewControllerAnimated:YES];

}






#pragma mark - private methods
- (void)judgeLoginState{
    
    if ([self.manger.loginSucceed intValue]>0) {
        self.loginBtn.backgroundColor = UIColorFromRGB(0xE97243);
        [self.loginBtn setTitle:@"注销" forState:UIControlStateNormal];
    } else {
//        self.loginBtn.backgroundColor = UIColorFromRGB(0x47B79F);
//        [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        self.loginBtn.hidden =  YES;
    }
}







#pragma mark - gettsers and setters








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
